﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;
using _2._23._2015_GroceryList.Data.Models;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity;

namespace _2._23._2015_GroceryList.Data
{
    public static class Seeder
    {
        public static void Seed(GroceryListDbContext db)
        {
            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(db);
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);

            RoleStore<Role> roleStore = new RoleStore<Role>(db);
            RoleManager<Role> roleManager = new RoleManager<Role>(roleStore);

            if (!roleManager.RoleExists("Admin"))
            {
                roleManager.Create(new Role { Name = "Admin" });
            }

            if (!roleManager.RoleExists("General"))
            {
                roleManager.Create(new Role { Name = "General" });
            }

            string nicksEmail = "nick@codercamps.com";
            ApplicationUser nick = userManager.FindByEmail(nicksEmail);
            
            if (nick == null)
            {
                nick = new ApplicationUser
                {
                    UserName = nicksEmail,
                    Email = nicksEmail
                };

                userManager.Create(nick, "123456");
                nick = userManager.FindByEmail(nicksEmail);

                userManager.AddToRole(nick.Id, "Admin");
            }
            else
            {
                if (!userManager.IsInRole(nick.Id, "Admin"))
                {
                    userManager.AddToRole(nick.Id, "Admin");
                }
            }

            string arunsEmail = "arun@codercamps.com";
            ApplicationUser arun = userManager.FindByEmail(arunsEmail);
            
            if (arun == null)
            {
                arun = new ApplicationUser
                {
                    UserName = arunsEmail,
                    Email = arunsEmail
                };

                userManager.Create(arun, "123456");
                arun = userManager.FindByEmail(arunsEmail);
                userManager.AddToRole(arun.Id, "General");
            }
            else
            {
                if (!userManager.IsInRole(arun.Id, "General"))
                {
                    userManager.AddToRole(arun.Id, "General");
                }
            }

            db.GroceryItemTypes.AddOrUpdate(t => t.Name,
                new GroceryItemType { Name = "Produce" },
                new GroceryItemType { Name = "Dairy" },
                new GroceryItemType { Name = "Other" });

            db.SaveChanges();

            db.GroceryItems.AddOrUpdate(i => i.ItemName,
                new GroceryItem { ItemName = "Peanut Butter",
                    ItemPrice = 3.99m, Quantity = 2, UserId = nick.Id,
                    GroceryItemTypeId = db.GroceryItemTypes.FirstOrDefault(t => t.Name == "Other").GroceryItemTypeId },
                new GroceryItem { ItemName = "Soy Sauce",
                    ItemPrice = 4.99m, Quantity = 1, UserId = nick.Id,
                    GroceryItemTypeId = db.GroceryItemTypes.FirstOrDefault(t => t.Name == "Other").GroceryItemTypeId },
                new GroceryItem { ItemName = "Siracha",
                    ItemPrice = 2.78m, Quantity = 2, UserId = arun.Id,
                    GroceryItemTypeId = db.GroceryItemTypes.FirstOrDefault(t => t.Name == "Other").GroceryItemTypeId },
                new GroceryItem { ItemName = "Milk",
                    ItemPrice = 2.99m, Quantity = 1, UserId = arun.Id,
                    GroceryItemTypeId = db.GroceryItemTypes.FirstOrDefault(t => t.Name == "Dairy").GroceryItemTypeId }
                );
        }
    }
}
